package com.cloudptt.api.product.ringer;

import android.content.Context;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.SoundPool;
import android.telephony.TelephonyManager;

import com.cloudptt.api.product.ISetSpeaker;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.local.SysConfig;

public class PromptTone {

	private final static String THIS_FILE = "PromptTone";
	public final static int Type_Disconnect = 1;
	public final static int Type_Upgrad = 2;
	public final static int Type_Normal = 3;
	public final static int Type_Low = 4;
	public final static int Type_Linking = 5;
	public final static int Type_NoLink = 6;
	public final static int Type_OpOver = 7;
	public final static int Type_OpFailed = 8;
	
	public final static int Type_On = 9;
	public final static int Type_Off = 10;
	private final static int Type_Err = 11;

	private float maxVol=1;

	private Context context = null;
	private ISetSpeaker speraker = null;
	private String lanType = "";
	private TelephonyManager telManager = null;

	private SoundPool pool;  
	private int disconnectId,updateId,lowId,normalId,linkingId,noHasSimId,noLinkId,btnOnId,btnOffId,errId = -1;
	private int opOverId,opFailedId = -1;

	public PromptTone(Context context,ISetSpeaker speraker,String lanType)
	{
		Log.d(THIS_FILE, "PromptTone");
		this.context = context;
		this.speraker = speraker;
		this.lanType = lanType;
		telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
//		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//		maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		pool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);  
		init();
		Log.d(THIS_FILE, "PromptTone end ");
	}

	private void init()
	{
		Log.d(THIS_FILE, "init");
//		int id = getSoundSourceId(true,"disconnect");
//		if(id > 0)
//		{
//			disconnectId = pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"update");
//		if(id > 0)
//		{
//			updateId = pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"low");
//		if(id > 0)
//		{
//			lowId =  pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"normal");
//		if(id > 0)
//		{
//			normalId =  pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"linking");
//		if(id > 0)
//		{
//			linkingId =  pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"nosim");
//		if(id > 0)
//		{
//			noHasSimId = pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"nolink");
//		if(id > 0)
//		{
//			noLinkId = pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"opover");
//		if(id > 0)
//		{
//			this.opOverId = pool.load(context, id, 1);
//		}
//		id = getSoundSourceId(true,"opfailed");
//		if(id > 0)
//		{
//			this.opFailedId = pool.load(context, id, 1);
//		}
		int id = getSoundSourceId(false,"on");
		if(id > 0)
		{
			btnOnId = pool.load(context, id, 1);
		}
		id = getSoundSourceId(false,"off");
		if(id > 0)
		{
			this.btnOffId = pool.load(context, id, 1);;
		}
		id = getSoundSourceId(false,"err");
		if(id > 0)
		{
			this.errId = pool.load(context, id, 1);;
		}
		
	}

	//	public void init(int disId,int upId,int lId,int nId,int lingId,int noSimId,int unLinkId,int btnUpId)
	//	{
	//		Log.d(THIS_FILE, "init");
	//		disconnectId = pool.load(context, disId, 1);
	//		updateId = pool.load(context, upId, 1);
	//		lowId =  pool.load(context, lId, 1);
	//		normalId =  pool.load(context, nId, 1);
	//		linkingId =  pool.load(context, lingId, 1);
	//		noHasSimId = pool.load(context, noSimId, 1);
	//		noLinkId = pool.load(context, unLinkId, 1);
	//		this.btnUpId = btnUpId;
	//	}

	private int getSoundSourceId(boolean isLan,String name)
	{
		if(isLan)
		{
			name = getSouceName(name);
		}
		String pkName = context.getPackageName();
		Resources res= context.getResources();
		return res.getIdentifier(name, "raw", pkName);
	}

	private String getSouceName(String name)
	{
		if(!SysConfig.LAN_ZH_CN.equals(lanType))
		{
			name = name + "_en";
		}
		return name;
	}

	public boolean canPlay(int type)
	{
		if(type == Type_Disconnect)
		{
			return true;
		}
		else if(type == Type_Upgrad)
		{
			return true;
		}
		else if(type == Type_Normal)
		{
			return true;
		}
		else if(type == Type_Low)
		{
			return true;
		}
		else if(type == Type_Linking)
		{
			return true;
		}
		else if(type == Type_NoLink)
		{
			return true;
		}
		else if(type == Type_OpOver)
		{
			return true;
		}
		else if(type == Type_OpFailed)
		{
			return true;
		}
		else if(type == Type_On)
		{
			return true;
		}
		else if(type == Type_Off)
		{
			return true;
		}
		else if(type == Type_Err)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
//	public boolean isBtnUp(int type)
//	{
//		if(btnUpId == type)
//		{
//			return true;
//		}
//		return false;
//	}
	public void soundRing(int type)
	{
		Log.d(THIS_FILE, "soundRing type = " + type);
		int soundSource = this.getPromptType(type);
		Log.d(THIS_FILE, "soundRing soundSource = " + soundSource);
		if(soundSource == -1)
		{
			return;
		}
		//		if( audioManager==null)
		//		{
		//			return;
		//		}
		//		boolean isOpen = audioManager.isSpeakerphoneOn();
		//		if(!isOpen)
		//		{
		//			audioManager.setSpeakerphoneOn(true);
		//		}
		speraker.setSpeaker(true);
		if(pool == null)
		{
			Log.d(THIS_FILE, "soundRing pool == null");
			return;
		}

		pool.play(soundSource, maxVol, maxVol, 65535, 0, 1);
//		
//		speraker.setSpeaker(false);
//		new Handler().postDelayed(new Runnable(){   
//
//		    public void run() 
//		    {   
//		    	speraker.setSpeaker(false);
//		    }   
//
//		 }, 1000);  
		
		//		if(!isOpen)
		//		{
		//			audioManager.setSpeakerphoneOn(false );
		//		}
	}
	
	

	public void onDestroy() 
	{
		if(pool != null)
		{
			pool.unload(disconnectId);
			pool.unload(updateId);
			pool.unload(normalId);
			pool.unload(lowId);
			pool.unload(linkingId);
			pool.unload(noHasSimId);
			pool.unload(noLinkId);
			pool.unload(opOverId);
			pool.unload(opFailedId);
			pool.unload(btnOnId);
			pool.unload(btnOffId);
			pool.unload(errId);
			pool.release();
			pool = null;
			speraker = null;
			telManager = null;
		}
	}
	private int getPromptType(int type)
	{
		if(type == Type_Disconnect)
		{
			boolean isHas = isHavSim();
			if(isHas)
			{
				return disconnectId;
			}
			else
			{
				return noHasSimId;
			}
		}
		else if(type == Type_Upgrad)
		{
			return updateId;
		}
		else if(type == Type_Normal)
		{
			return normalId;
		}
		else if(type == Type_Low)
		{
			return lowId;
		}
		else if(type == Type_Linking)
		{
			return linkingId;
		}
		else if(type == Type_NoLink)
		{
			return noLinkId;
		}
		else if(type == Type_OpOver)
		{
			return opOverId;
		}
		else if(type == Type_OpFailed)
		{
			return opFailedId;
		}
		else if(type == Type_On)
		{
			return btnOnId;
		}
		else if(type == Type_Off)
		{
			return btnOffId;
		}
		else if(type == Type_Err)
		{
			return errId;
		}
		else
		{
			return -1;
		}
	}

	private boolean isHavSim()
	{
		int state = telManager.getSimState();
		if(TelephonyManager.SIM_STATE_UNKNOWN == state
				||TelephonyManager.SIM_STATE_ABSENT == state)
		{
			return false;
		}
		return true;
	}

	public void setLanType(String lanType) {
		this.lanType = lanType;
		init();
	}
}
